<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <!-- 更改用户头像 -->
    <!-- 先设定一个图片的输入框,然后点击头像单元格的时候触发 -->
    <input type="file" hidden ref="file" @change="onFileChange" />

    <van-cell title="头像" @click="$refs.file.click()" is-link>
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>

    <van-cell
      title="昵称"
      @click="isShowUpdateName = true"
      :value="user.name"
      is-link
    />
    <van-cell
      title="性别"
      :value="user.gender ? '女' : '男'"
      is-link
      @click="isShowUpdateGender = true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isShowUpdateBirthday = true"
    />
    <!-- 编辑昵称弹层 -->
    <van-popup
      v-if="isShowUpdateName"
      v-model="isShowUpdateName"
      style="height: 100%"
      position="bottom"
    >
      <UpdateName
        v-model="user.name"
        v-if="isShowUpdateName"
        @close="isShowUpdateName = false"
      />
    </van-popup>
    <!-- 编辑昵称弹层 -->
    <!-- 编辑性别弹层 -->
    <van-popup
      v-if="isShowUpdateGender"
      v-model="isShowUpdateGender"
      position="bottom"
    >
      <UpdateGender v-model="user.gender" @close="isShowUpdateGender = false" />
    </van-popup>
    <!-- 编辑生日的弹层 -->
    <van-popup v-model="isShowUpdateBirthday" position="bottom">
      <UpdateBirthday
        v-if="isShowUpdateBirthday"
        v-model="user.birthday"
        @close="isShowUpdateBirthday = false"
      />
    </van-popup>
    <!-- 修改头像的弹层 -->
    <van-popup v-model="isShowUpdateImg" style="height: 100%" position="bottom">
      <UpdateImg
        :img="img"
        @close="isShowUpdateImg = false"
        @update-photo="user.photo = $event"
      />
    </van-popup>
  </div>
</template>
<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdateImg from './components/update-img.vue'
export default {
  data() {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthday: false,
      isShowUpdateImg: false,
      img: null
    }
  },
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdateImg
  },
  created() {
    this.loadUser()
  },
  methods: {
    async loadUser() {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
      } catch (err) {
        this.$toast('获取失败')
      }
    },
    onFileChange() {
      // 获取文件对象
      const file = this.$refs.file.files[0]

      // 基于文章对象获取 blob 数据
      this.img = window.URL.createObjectURL(file)

      // 展示预览图片弹出层
      this.isShowUpdateImg = true

      // file-input 如果选了同一个文件不会触发 change 事件
      // 解决办法就是每次使用完毕，把它的 value 清空
      this.$refs.file.value = ''
    }
  }
}
</script>
<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>
